package com.lemzdo.mail.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.lemzdo.mail.product.vo.AttrRespVo;
import com.lemzdo.mail.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lemzdo.mail.product.entity.AttrEntity;
import com.lemzdo.mail.product.service.AttrService;
import com.lemzdo.common.utils.PageUtils;
import com.lemzdo.common.utils.R;



/**
 * 
 *
 * @author lemZdo
 * @email sunlightcs@gmail.com
 * @date 2020-06-18 13:11:44
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;


    /**
     *      获取分类规格参数（基本属性） 或者 销售属性
     * product/attr/base/list/{catelogId}
     * /product/attr/sale/list/{catelogId}
     * @param params 分页参数
     * @param catelogId 三级分类id
     * @return list
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable Long catelogId,
                      @PathVariable String attrType){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,attrType);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     * /product/attr/info/{attrId}
     * 返回值用AttrEntity接受不够 前端还需要其他数据(catelogPath) 封装在AttrRespVo中
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){

		//AttrEntity attr = attrService.getById(attrId);
        //自定义返回数据
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     * 完善，使用VO来接收前端传回来的数据
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo){

		attrService.saveAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
